需要在response中添加 response.addHeader("content-length",length); length代表文件长度 获取wb长度的方法 ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); int length = baos.size(); response.addHeader("content-length",length+""); 完整方法如下
public static void downloadExcel(HttpServletResponse response,Workbook wb,String fileName) throws Exception{ try { response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso-8859-1")); response.setContentType("application/ynd.ms-excel;charset=UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); int length = baos.size(); response.addHeader("content-length",length+""); OutputStream out=response.getOutputStream(); wb.write(out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }